package kokist.android.shichaanimator;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;

/**
 * Created by  Hao on 2015/5/27.
 * Email  695686241@qq.com
 */
public class ParallaxLayoutInflater extends LayoutInflater {


    public ParallaxLayoutInflater(LayoutInflater orginla, Context newContext) {
        super(orginla, newContext);
        setFactory(new ParllaxFactory(this));
    }

    @Override
    public LayoutInflater cloneInContext(Context newContext) {
        return new ParallaxLayoutInflater(this, newContext);
    }

    class ParllaxFactory implements LayoutInflater.Factory {
        private LayoutInflater layoutInflater;
        private final String[] sClassPrefix = {
                "android.widegt.",
                "android.view."
        };

        public ParllaxFactory(LayoutInflater layoutInflater) {
            this.layoutInflater = layoutInflater;
        }

        //自定义视图创建过程
        @Override
        public View onCreateView(String name, Context context, AttributeSet attrs) {
//           layoutInflater.createView(name,)
            View view = null;
            if (view == null) {
                view = CreateViewOrFailQeitly(name, context, attrs);
            }
            if (view!=null){
                //实例化完成,获取自定义属性,通过标签关联到视图上；
               setViewTag(view, context, attrs);
            }
            return view;
        }

        private void setViewTag(View view, Context context, AttributeSet attrs) {
          int[] attrids={
            R.attr.a_in,
                  R.attr.a_out,
                  R.attr.x_in,
                  R.attr.x_out,
                  R.attr.y_in,
                  R.attr.y_out
          };
            TypedArray a=context.obtainStyledAttributes(attrs,attrids);
            if (a!=null&&a.length()>0){
             ParallaxViewTag tag=new ParallaxViewTag();
                tag.alphaIn=a.getFloat(0,0f);
                tag.alphaOut=a.getFloat(1,0f);
                tag.xIn=a.getFloat(2,0f);
                tag.xOut=a.getFloat(3,0f);
                tag.yIn=a.getFloat(4,0f);
                tag.yOut=a.getFloat(5,0f);
                view.setTag(R.id.parallax_view_tag,tag);
            }
            a.recycle();
        }

        private View CreateViewOrFailQeitly(String name, String prefix, Context context, AttributeSet attrs) {
            try {
                return layoutInflater.createView(name, prefix, attrs);
            } catch (Exception e) {
                return null;
            }
        }

        private View CreateViewOrFailQeitly(String name, Context context, AttributeSet attrs) {
            //如果是自定义控件
            if (name.contains(".")) {
               return   CreateViewOrFailQeitly(name,null,context,attrs);
            }
                //系统视图
             for (String prefix:sClassPrefix){
                View view=  CreateViewOrFailQeitly(name,prefix,context,attrs);
                 if (view!=null){
                       return  view;
                 }
             }
            return null;
        }
    }
}
